#pragma once

#include "bitmap.h"
#include "zoomviewpanel.h"
#include "tool.h"

class BMPZoomViewPanel : public ZoomViewPanel
{
protected:
	/** what image is being drawn */
	BitMaP * m_bmp;
	/** which color to draw transparent */
	int * m_transparentColor;
public:
	inline BitMaP * getBMP(){return m_bmp;}
	BMPZoomViewPanel():m_bmp(0){}
	void set(BitMaP * a_bmp, int * a_transparentColor)
	{
		m_bmp = a_bmp;
		m_transparentColor = a_transparentColor;
		setArea(SRect(SPoint(), a_bmp->getDimension()));
	}
	void draw(SDL_Surface * a_surface)
	{
		if(m_bmp)
		{
		// how much of the screen we'd like to draw in (the full area)
		SRect drawArea(*getArea());
		// how much of the screen we can actually draw in (the clipped area)
		SRect viewArea = VSDL_getClip(a_surface);
		// the position offset between where we want to start drawing, and where we are drawing
		SPoint scroll = viewArea.getPosition().difference(drawArea.getPosition());
		// the size of the un-zoomed image, should be the same as drawArea.getDimension().quotient(m_zoom)
		SPoint bmpDimension(m_bmp->getDimension());
		// how much of the image is actually viewable
		SPoint viewAreaDimensions(viewArea.getDimension());
		// where in the unzoomed image to start grabbing pixels from
		SPoint scrollFromImage(scroll.quotient(m_zoom));
		// how many pixels from the unzoomed image to grab
		SPoint dimensionsFromImage(viewAreaDimensions.quotient(m_zoom));
		// which pixels from the unzoomed image to draw
		//SRect pixelsFromImageToDraw(0,0,bmpDimension.getX(),bmpDimension.getY());
		SRect pixelsFromImageToDraw(scrollFromImage.getX(),scrollFromImage.getY(),
			dimensionsFromImage.getX(),dimensionsFromImage.getY());
		// where in the draw area to start drawing the image
		//SPoint whereToStartDrawingTheImage(drawArea.getPosition());
		SPoint whereToStartDrawingTheImage(viewArea.getPosition());

		// account for pixel scrolling
		whereToStartDrawingTheImage.x -= scroll.getX() % m_zoom;
		whereToStartDrawingTheImage.y -= scroll.getY() % m_zoom;

		// account for missing pixels due to scrolling
		pixelsFromImageToDraw.setWidth(
			pixelsFromImageToDraw.getWidth()+1);
		pixelsFromImageToDraw.setHeight(
			pixelsFromImageToDraw.getHeight()+1);

		// what is the first pixel that is calculated to draw
		SPoint minPixels(pixelsFromImageToDraw.getMin());
		// if the first pixels are before the image...
		if(minPixels.getX() < 0){
			whereToStartDrawingTheImage.addX(-minPixels.getX()*m_zoom);
			pixelsFromImageToDraw.setX(0);
		}if(minPixels.getY() < 0){
			whereToStartDrawingTheImage.addY(-minPixels.getY()*m_zoom);
			pixelsFromImageToDraw.setY(0);
		}
		// what is the last pixel that is calculated to draw
		SPoint maxPixels(pixelsFromImageToDraw.getMax());
		// if the last pixels are after the image...
		if(maxPixels.getX() >= bmpDimension.getX()){
			pixelsFromImageToDraw.setWidth(bmpDimension.getX()-pixelsFromImageToDraw.getX());
		}if(maxPixels.getY() >= bmpDimension.getY()){
			pixelsFromImageToDraw.setHeight(bmpDimension.getY()-pixelsFromImageToDraw.getY());
		}

		VSDL_drawBitmapToSurface(a_surface, 
			whereToStartDrawingTheImage, 
			m_bmp, 
			pixelsFromImageToDraw, 
			0, m_zoom, *m_transparentColor);
		}
		if(m_tool)
			m_tool->draw(a_surface);
	}

	bool handleEvent(const SDL_Event & e)
	{
		bool handled = false;
		if(m_tool)
			handled = m_tool->handleEvent(e);
		handled |= ZoomViewPanel::handleEvent(e);
		return handled;
	}
};